package com.blacktea.structuralpatterns.flyweightpattern;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 创建享元工厂
 * @author: black tea
 * @date: 2023/3/23 14:02
 */
@Component
public class FlyweightFactory {

    private Map<String, Flyweight> flyweightMap = new HashMap<>();

    public Flyweight getFlyweight(String intrinsicState) {
        if (!flyweightMap.containsKey(intrinsicState)) {
            flyweightMap.put(intrinsicState, new ConcreteFlyweight(intrinsicState));
        }
        return flyweightMap.get(intrinsicState);
    }

}
